/*
 * Decompiled with CFR 0.150.
 * 
 * Could not load the following classes:
 *  cn.hutool.core.date.DateUtil
 *  cn.hutool.core.util.ObjectUtil
 *  de.felixroske.jfxsupport.FXMLController
 *  javafx.application.Platform
 *  javafx.beans.value.ChangeListener
 *  javafx.beans.value.ObservableValue
 *  javafx.collections.FXCollections
 *  javafx.collections.ObservableList
 *  javafx.event.EventHandler
 *  javafx.fxml.FXML
 *  javafx.fxml.Initializable
 *  javafx.scene.Node
 *  javafx.scene.control.Alert
 *  javafx.scene.control.Alert$AlertType
 *  javafx.scene.control.TableCell
 *  javafx.scene.control.TableColumn
 *  javafx.scene.control.TableView
 *  javafx.scene.control.TextField
 *  javafx.scene.control.cell.PropertyValueFactory
 *  javafx.scene.input.KeyCode
 *  javafx.scene.input.KeyEvent
 *  javafx.scene.layout.AnchorPane
 *  javafx.stage.Stage
 *  javafx.util.Callback
 *  org.springframework.beans.factory.annotation.Autowired
 */
package com.gys.controller.salesReceipts;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.gys.common.business.SalesReceiptsUtil;
import com.gys.common.data.HttpJson;
import com.gys.common.util.CacheUtil;
import com.gys.common.util.CommonUtil;
import com.gys.common.util.SpringUtil;
import com.gys.controller.BaseController;
import com.gys.controller.salesReceipts.SalesReceiptsMainController;
import com.gys.data.example.GetUserOutData;
import com.gys.data.salesReceipts.CardInfo;
import com.gys.data.salesReceipts.GaiaSdPromCouponBasic;
import com.gys.data.salesReceipts.GetGiftCardOutData;
import com.gys.data.salesReceipts.GetQueryMemberOutData;
import com.gys.data.salesReceipts.GiftCardSet;
import com.gys.data.salesReceipts.GiveGiftCardInfo;
import com.gys.view.salesReceipts.GiftCardView;
import de.felixroske.jfxsupport.FXMLController;
import java.math.BigDecimal;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Timer;
import java.util.TimerTask;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Alert;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.util.Callback;
import org.springframework.beans.factory.annotation.Autowired;

@FXMLController
public class GiftCardController
extends BaseController
implements Initializable {
    @FXML
    private AnchorPane pane;
    @FXML
    private TableView<GiveGiftCardInfo> tableView;
    @FXML
    private TableColumn<GiveGiftCardInfo, String> indexF;
    @FXML
    private TableColumn<GiveGiftCardInfo, String> startDataF;
    @FXML
    private TableColumn<GiveGiftCardInfo, String> weekF;
    @FXML
    private TableColumn<GiveGiftCardInfo, String> endTimeF;
    @FXML
    private TableColumn<GiveGiftCardInfo, String> proCodeS;
    @FXML
    private TableColumn<GiveGiftCardInfo, String> num;
    @FXML
    private TableColumn<GiveGiftCardInfo, String> promotionNameF;
    @FXML
    private TableColumn<GiveGiftCardInfo, String> gspcgActNoF;
    @FXML
    private TableColumn<GiveGiftCardInfo, String> dataF;
    @FXML
    private TableColumn<GiveGiftCardInfo, String> voucherIdF;
    @FXML
    private TableColumn<GiveGiftCardInfo, String> maxNum;
    @FXML
    private TableColumn<GiveGiftCardInfo, String> startTimeF;
    @FXML
    private TableColumn<GiveGiftCardInfo, String> promotionContentF;
    @FXML
    private TableColumn<GiveGiftCardInfo, String> endDataF;
    @FXML
    private TableColumn<GiveGiftCardInfo, String> proNameS;
    @FXML
    private TableView<CardInfo> tableViewTwo;
    @FXML
    private TableColumn<CardInfo, String> indexS;
    @FXML
    private TableColumn<CardInfo, String> gspcsCouponRemarks;
    @FXML
    private TableColumn<CardInfo, String> gspcsSingleUseAmt;
    @FXML
    private TableColumn<CardInfo, String> gspcsBeginTime;
    @FXML
    private TableColumn<CardInfo, String> gspcsEndDate;
    @FXML
    private TableColumn<CardInfo, String> gspcsEndTime;
    @FXML
    private TableColumn<CardInfo, String> numS;
    @FXML
    private TableColumn<CardInfo, String> gspcgActNoS;
    @FXML
    private TableColumn<CardInfo, String> gspcsCouponType;
    @FXML
    private TableColumn<CardInfo, String> gspcsBeginDate;
    @FXML
    private TableColumn<CardInfo, String> gspcsSinglePaycheckAmt;
    @Autowired
    private SalesReceiptsMainController salesReceiptsMainController;
    public ObservableList<GiveGiftCardInfo> observableTableList = FXCollections.observableArrayList(new ArrayList());
    public ObservableList<CardInfo> observableTableTwoList = FXCollections.observableArrayList(new ArrayList());
    public List<GetGiftCardOutData> getGiftCardOutData = new ArrayList<GetGiftCardOutData>();

    public void initialize(URL location, ResourceBundle resources) {
        this.bangding();
        this.addNumSTextField(this.numS);
        this.init();
    }

    @Override
    public void init() {
        if (ObjectUtil.isEmpty(this.pane)) {
            return;
        }
        this.observableTableList.clear();
        this.tableView.refresh();
        this.observableTableTwoList.clear();
        this.tableViewTwo.refresh();
        this.initTable();
        this.tableClick();
    }

    private void bangding() {
        this.indexF.setCellValueFactory((Callback)new PropertyValueFactory(this.indexF.getId()));
        this.startDataF.setCellValueFactory((Callback)new PropertyValueFactory(this.startDataF.getId()));
        this.weekF.setCellValueFactory((Callback)new PropertyValueFactory(this.weekF.getId()));
        this.endTimeF.setCellValueFactory((Callback)new PropertyValueFactory(this.endTimeF.getId()));
        this.proCodeS.setCellValueFactory((Callback)new PropertyValueFactory(this.proCodeS.getId()));
        this.num.setCellValueFactory((Callback)new PropertyValueFactory(this.num.getId()));
        this.promotionNameF.setCellValueFactory((Callback)new PropertyValueFactory(this.promotionNameF.getId()));
        this.gspcgActNoF.setCellValueFactory((Callback)new PropertyValueFactory(this.gspcgActNoF.getId()));
        this.dataF.setCellValueFactory((Callback)new PropertyValueFactory(this.dataF.getId()));
        this.voucherIdF.setCellValueFactory((Callback)new PropertyValueFactory(this.voucherIdF.getId()));
        this.maxNum.setCellValueFactory((Callback)new PropertyValueFactory(this.maxNum.getId()));
        this.startTimeF.setCellValueFactory((Callback)new PropertyValueFactory(this.startTimeF.getId()));
        this.promotionContentF.setCellValueFactory((Callback)new PropertyValueFactory(this.promotionContentF.getId()));
        this.endDataF.setCellValueFactory((Callback)new PropertyValueFactory(this.endDataF.getId()));
        this.proNameS.setCellValueFactory((Callback)new PropertyValueFactory(this.proNameS.getId()));
        this.indexS.setCellValueFactory((Callback)new PropertyValueFactory(this.indexS.getId()));
        this.gspcsCouponRemarks.setCellValueFactory((Callback)new PropertyValueFactory(this.gspcsCouponRemarks.getId()));
        this.gspcsSingleUseAmt.setCellValueFactory((Callback)new PropertyValueFactory(this.gspcsSingleUseAmt.getId()));
        this.gspcsBeginTime.setCellValueFactory((Callback)new PropertyValueFactory(this.gspcsBeginTime.getId()));
        this.gspcsEndDate.setCellValueFactory((Callback)new PropertyValueFactory(this.gspcsEndDate.getId()));
        this.gspcsEndTime.setCellValueFactory((Callback)new PropertyValueFactory(this.gspcsEndTime.getId()));
        this.numS.setCellValueFactory((Callback)new PropertyValueFactory(this.numS.getId()));
        this.gspcgActNoS.setCellValueFactory((Callback)new PropertyValueFactory(this.gspcgActNoS.getId()));
        this.gspcsCouponType.setCellValueFactory((Callback)new PropertyValueFactory(this.gspcsCouponType.getId()));
        this.gspcsBeginDate.setCellValueFactory((Callback)new PropertyValueFactory(this.gspcsBeginDate.getId()));
        this.gspcsSinglePaycheckAmt.setCellValueFactory((Callback)new PropertyValueFactory(this.gspcsSinglePaycheckAmt.getId()));
    }

    private void initTable() {
        ArrayList<GiveGiftCardInfo> giveGiftCardInfos = new ArrayList<GiveGiftCardInfo>();
        for (int i = 0; i < this.getGiftCardOutData.size(); ++i) {
            GiveGiftCardInfo giftCardInfo = new GiveGiftCardInfo();
            giftCardInfo.setIndexF(String.valueOf(i + 1));
            this.covert(giftCardInfo, this.getGiftCardOutData.get(i));
            giveGiftCardInfos.add(giftCardInfo);
        }
        this.observableTableList.addAll(giveGiftCardInfos);
        this.tableView.setItems(this.observableTableList);
    }

    private void tableClick() {
        this.tableView.getSelectionModel().selectedItemProperty().addListener((ChangeListener)new ChangeListener<GiveGiftCardInfo>(){

            public void changed(ObservableValue<? extends GiveGiftCardInfo> observableValue, GiveGiftCardInfo oldItem, GiveGiftCardInfo newItem) {
                if (ObjectUtil.isEmpty(newItem)) {
                    return;
                }
                GiftCardController.this.observableTableTwoList.clear();
                if (ObjectUtil.isNotEmpty(newItem.getCardInfoList())) {
                    GiftCardController.this.observableTableTwoList = FXCollections.observableArrayList(newItem.getCardInfoList());
                    GiftCardController.this.tableViewTwo.setItems(GiftCardController.this.observableTableTwoList);
                }
                GiftCardController.this.tableViewTwo.refresh();
            }
        });
    }

    private void covert(GiveGiftCardInfo giftCardInfo, GetGiftCardOutData getGiftCardOutData) {
        giftCardInfo.setGiveType(getGiftCardOutData.getSameOrDiff());
        giftCardInfo.setStartDataF(getGiftCardOutData.getGsphBeginDate());
        giftCardInfo.setWeekF(getGiftCardOutData.getGsphWeekFrequency());
        giftCardInfo.setEndTimeF(getGiftCardOutData.getGsphEndTime());
        giftCardInfo.setProCodeS(getGiftCardOutData.getProId());
        giftCardInfo.setPromotionNameF(getGiftCardOutData.getPromotionName());
        giftCardInfo.setGspcgActNoF(getGiftCardOutData.getGspcgActNo());
        giftCardInfo.setDataF(getGiftCardOutData.getGsphDateFrequency());
        giftCardInfo.setVoucherIdF(getGiftCardOutData.getGsphVoucherId());
        giftCardInfo.setMaxNum(getGiftCardOutData.getTotalNum());
        giftCardInfo.setStartTimeF(getGiftCardOutData.getGsphBeginTime());
        giftCardInfo.setPromotionContentF(getGiftCardOutData.getPromotionContent());
        giftCardInfo.setEndDataF(getGiftCardOutData.getGsphEndDate());
        giftCardInfo.setProNameS(getGiftCardOutData.getProName());
        ArrayList<CardInfo> cardInfoList = new ArrayList<CardInfo>();
        for (int i = 0; i < getGiftCardOutData.getGiftCardSetList().size(); ++i) {
            GiftCardSet giftCardSet = getGiftCardOutData.getGiftCardSetList().get(i);
            CardInfo cardInfo = new CardInfo();
            cardInfo.setIndexS(String.valueOf(i + 1));
            cardInfo.setGspcsCouponRemarks(giftCardSet.getGspcsCouponRemarks());
            cardInfo.setGspcsSingleUseAmt(giftCardSet.getGspcsSingleUseAmt());
            cardInfo.setGspcsBeginTime(giftCardSet.getGspcsBeginTime());
            cardInfo.setGspcsEndDate(giftCardSet.getGspcsEndDate());
            cardInfo.setGspcsEndTime(giftCardSet.getGspcsEndTime());
            cardInfo.setGspcgActNoS(giftCardSet.getGspcgActNo());
            cardInfo.setGspcsCouponType(giftCardSet.getGspcsCouponType());
            cardInfo.setGspcsBeginDate(giftCardSet.getGspcsBeginDate());
            cardInfo.setGspcsSinglePaycheckAmt(giftCardSet.getGspcsSinglePaycheckAmt());
            cardInfoList.add(cardInfo);
        }
        giftCardInfo.setCardInfoList(cardInfoList);
    }

    private void addNumSTextField(TableColumn<CardInfo, String> column) {
        column.setCellFactory(tc -> {
            final TextField textField = new TextField();
            TableCell<CardInfo, String> cell = new TableCell<CardInfo, String>(){

                protected void updateItem(String chuzhi, boolean empty) {
                    super.updateItem(chuzhi, empty);
                    if (empty) {
                        this.setGraphic(null);
                    } else {
                        this.setGraphic((Node)textField);
                    }
                }
            };
            textField.textProperty().addListener((ChangeListener)new ChangeListener<String>(){
                final  TableCell val_cell;
                final  TextField val_textField;
                {
                    this.val_cell = cell;
                    this.val_textField = textField;
                }

                public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                    GiveGiftCardInfo selectedItem = (GiveGiftCardInfo)GiftCardController.this.tableView.getSelectionModel().getSelectedItem();
                    int index = this.val_cell.getIndex();
                    CardInfo cardInfo = (CardInfo)GiftCardController.this.tableViewTwo.getItems().get(index);
                    if (ObjectUtil.isEmpty(newValue)) {
                        cardInfo.setNumS(newValue);
                        int totalNum = GiftCardController.this.countTotal();
                        selectedItem.setNum(String.valueOf(totalNum));
                        GiftCardController.this.tableView.refresh();
                        return;
                    }
                    if (ObjectUtil.isNotEmpty(newValue) && !CommonUtil.positiveInteger(newValue)) {
                        this.val_textField.setText(oldValue);
                        return;
                    }
                    String maxNum = selectedItem.getMaxNum();
                    cardInfo.setNumS(newValue);
                    int totalNum = GiftCardController.this.countTotal();
                    if (Integer.valueOf(maxNum) < Integer.valueOf(totalNum)) {
                        this.val_textField.setText(oldValue);
                        cardInfo.setNumS(oldValue);
                        Alert alert = GiftCardController.this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", "\u8d85\u51fa\u6700\u5927\u53ef\u9001\u6570\u91cf");
                        alert.showAndWait();
                        return;
                    }
                    selectedItem.setNum(String.valueOf(totalNum));
                    GiftCardController.this.tableView.refresh();
                }
            });
            return cell;
        });
    }

    private int countTotal() {
        int totalNum = 0;
        for (int i = 0; i < this.tableViewTwo.getItems().size(); ++i) {
            CardInfo cardInfo1 = (CardInfo)this.tableViewTwo.getItems().get(i);
            if (ObjectUtil.isEmpty(cardInfo1.getNumS())) continue;
            totalNum += Integer.valueOf(cardInfo1.getNumS()).intValue();
        }
        return totalNum;
    }

    private void menberTip() {
        new Timer().schedule(new TimerTask(){

            @Override
            public void run() {
                GiftCardView view = SpringUtil.getBean(GiftCardView.class);
                Stage stage = (Stage)view.getView().getScene().getWindow();
                stage.getScene().setOnKeyReleased((EventHandler)new EventHandler<KeyEvent>(){

                    public void handle(KeyEvent e) {
                        if (e.getCode() == KeyCode.H) {
                            GiftCardController.this.salesReceiptsMainController.inputMember();
                        }
                        e.consume();
                    }
                });
                Platform.runLater((Runnable)new Runnable(){

                    @Override
                    public void run() {
                        Alert alert = GiftCardController.this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", "\u8bf7\u8f93\u5165\u4f1a\u5458\u4fe1\u606f,\u5feb\u6377\u952e<H>");
                        alert.showAndWait();
                    }
                });
            }
        }, 500L);
    }

    @FXML
    private void sureAction() {
        GetQueryMemberOutData memberOutData = (GetQueryMemberOutData)CacheUtil.get("memberOutData");
        if (ObjectUtil.isEmpty(memberOutData)) {
            this.menberTip();
            return;
        }
        GetUserOutData loginUser = this.getLoginUser();
        ArrayList<GaiaSdPromCouponBasic> promCouponBasicList = new ArrayList<GaiaSdPromCouponBasic>();
        ObservableList items = this.tableView.getItems();
        for (int i = 0; i < items.size(); ++i) {
            GiveGiftCardInfo giftCardInfo = (GiveGiftCardInfo)items.get(i);
            List<CardInfo> cardInfoList = giftCardInfo.getCardInfoList();
            for (int j = 0; j < cardInfoList.size(); ++j) {
                CardInfo cardInfo = cardInfoList.get(j);
                String numS = cardInfo.getNumS();
                if (ObjectUtil.isEmpty(numS) || Integer.valueOf(numS) == 0) continue;
                int num = Integer.valueOf(numS);
                for (int k = 0; k < num; ++k) {
                    GaiaSdPromCouponBasic couponBasic = new GaiaSdPromCouponBasic();
                    couponBasic.setClientId(loginUser.getClient());
                    couponBasic.setGspcbActNo(cardInfo.getGspcgActNoS());
                    couponBasic.setGspcbCouponAmt(new BigDecimal(cardInfo.getGspcsSinglePaycheckAmt()));
                    couponBasic.setGspcbMemberId(memberOutData.getCardNum());
                    couponBasic.setGspcbGainFlag("0");
                    couponBasic.setGspcbGrantBillNo(this.salesReceiptsMainController.getCaseId().getText());
                    couponBasic.setGspcbGrantBrId(loginUser.getDepId());
                    couponBasic.setGspcbGrantDate(DateUtil.format((Date)DateUtil.date(), (String)"yyyyMMdd"));
                    couponBasic.setGspcbStatus("0");
                    promCouponBasicList.add(couponBasic);
                }
            }
        }
        if (promCouponBasicList.size() == 0) {
            Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", "\u5f53\u524d\u8d60\u9001\u7535\u5b50\u5238\u6570\u91cf\u4e3a0");
            alert.showAndWait();
            return;
        }
        HashMap<String, Object> param = new HashMap<String, Object>();
        param.put("promCouponBasicList", promCouponBasicList);
        HttpJson httpJson = SalesReceiptsUtil.giveGiftCard(param);
        if (!httpJson.isSuccess()) {
            Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", httpJson.getMsg());
            alert.showAndWait();
            return;
        }
        this.cancelAction();
    }

    @FXML
    private void cancelAction() {
        this.pane.getScene().getWindow().hide();
    }
}

